package com.school.service.impl;

import com.school.common.ServiceResultEnum;
import com.school.controller.vo.IndexConfigGoodsVO;
import com.school.entity.Goods;
import com.school.entity.IndexConfig;
import com.school.mapper.GoodsMapper;
import com.school.mapper.IndexConfigMapper;
import com.school.service.IndexConfigService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IndexConfigServiceImpl implements IndexConfigService {
    @Resource
    private IndexConfigMapper indexConfigMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public PageResult getConfigsPage(PageQueryUtil pageQueryUtil) {
        int totalCount = indexConfigMapper.getTotalIndexConfigCount(pageQueryUtil);
        List<IndexConfig> indexConfigs = indexConfigMapper.findIndexConfigList(pageQueryUtil);
        PageResult pageResult = new PageResult(indexConfigs, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());

        return pageResult;
    }
    @Override
    public String saveIndexConfig(IndexConfig indexConfig) {
        IndexConfig indexConfig1 = indexConfigMapper.selectByGoodIdAndType(indexConfig.getGoodsId(),indexConfig.getConfigType());
        if(indexConfig1!=null){
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }
        int count=indexConfigMapper.insertSelective(indexConfig);
        if(count>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateIndexConfig(IndexConfig indexConfig) {
        IndexConfig temp=indexConfigMapper.selectByPrimaryKey(indexConfig.getConfigId());
        if(temp==null){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        IndexConfig temp2=indexConfigMapper.selectByGoodIdAndType(indexConfig.getGoodsId(),indexConfig.getConfigType());
        if(temp2!=null&&temp2.getConfigId().longValue()!=indexConfig.getConfigId().longValue()){
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }
        indexConfig.setCreateTime(new Date());
        if(indexConfigMapper.updateByPrimaryKeySelective(indexConfig)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String deleteBatch(Integer[] ids) {
        if (ids.length<1){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        int count = indexConfigMapper.deleteBatch(ids);
        if (count>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public List<IndexConfigGoodsVO> getConfigGoodsForIndex(int configType, int number) {
        List<IndexConfigGoodsVO> indexConfigGoodsVOS =new ArrayList<>();
        List<IndexConfig> indexConfigs=indexConfigMapper.findIndexConfigByTypeAndNum(configType, number);
        if(indexConfigs!=null){
            List<Long> goodsIds=new ArrayList<>();
            for(IndexConfig indexConfig:indexConfigs){
                goodsIds.add(indexConfig.getGoodsId());
            }
            List<Goods> goods=goodsMapper.selectByPrimaryKeys(goodsIds);
            indexConfigGoodsVOS = BeanUtil.copyList(goods,IndexConfigGoodsVO.class);
            for (IndexConfigGoodsVO indexConfigGoodsVO:indexConfigGoodsVOS){
                String goodsName=indexConfigGoodsVO.getGoodsName();
                String goodsIntro=indexConfigGoodsVO.getGoodsIntro();
                if(goodsName.length()>30){
                    goodsName=goodsName.substring(0,30)+"...";
                    indexConfigGoodsVO.setGoodsName(goodsName);
                }
                if(goodsIntro.length()>20){
                    goodsIntro=goodsIntro.substring(0,20)+"...";
                    indexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return indexConfigGoodsVOS;
    }

}